package com.zwan.oam_rtc.utils.messages;

import java.nio.ByteBuffer;


public class StringUtil {
	/**
	 * 防止SQL注入，对引号做特殊处理
	 * @param s 源字符串
	 * @return 已处理后
	 */
    public static String quotedStr(String s) {
        if (s == null)
            return "\'\'";
        StringBuffer sb = new StringBuffer(s.trim().replaceAll("\'", "''"));
        sb.insert(0, '\'');
        sb.append("\'");
        return sb.toString();
    }

    public static String clearStr(String s) {
        if (s == null)
            return "";
        return s.trim().replaceAll("\'", "''");
    }

    public static boolean isDigit(String s, int maxlength) {
        if (s == null || s.length() == 0)
            return false;
        return s.matches("[\\-\\+]?[0-9]{1," + maxlength + "}");
    }

    public static boolean isDigit(String s) {
        return isDigit(s, 99);
    }

//    public static void main(String[] args) {
//        System.out.println(isDigit("-09876543-67896678", 20));
//    }
    
    /**
     * 解析字符串为对象
     * @param type 目标对象类
     * @param value 字符串表示的对象值
     * @return 目标对象
     */
    public static Object parseObject(Class<?> type, String value){
		if (type == null || type.equals(String.class)) {
			return value;
        } else if (type.isArray()) {
            Class<?> arrayType = type.getComponentType();
            String[] arrayValue = value.split(",");
            Object[] arrayObj = null;
            if (arrayType.equals(Long.class)){
            	arrayObj = new Long[arrayValue.length];
            	for (int i = 0; i < arrayObj.length; i++) {
					arrayObj[i] = Long.parseLong(arrayValue[i]);
				}
            }
            else if (arrayType.equals(Integer.class)){
            	arrayObj = new Integer[arrayValue.length];
            	for (int i = 0; i < arrayObj.length; i++) {
					arrayObj[i] = Integer.parseInt(arrayValue[i]);
				}
            }
            else if (arrayType.equals(Short.class)){
            	arrayObj = new Short[arrayValue.length];
            	for (int i = 0; i < arrayObj.length; i++) {
					arrayObj[i] = Short.parseShort(arrayValue[i]);
				}
            }
            else if (arrayType.equals(Byte.class)){
            	arrayObj = new Byte[arrayValue.length];
            	for (int i = 0; i < arrayObj.length; i++) {
					arrayObj[i] = Byte.parseByte(arrayValue[i]);
				}
            }
            return arrayObj;
        } else if (type.equals(Long.class)) {
            return Long.parseLong(value);
        } else if (type.equals(Integer.class)) {
            return Integer.parseInt(value);
        } else if (type.equals(Short.class)) {
            return Short.parseShort(value);
        } else if (type.equals(Byte.class)) {
            return Byte.parseByte(value);
        }
		
		return null;
	}

    /**
     * 将指定对象放入字节流
     * @param v 对象值
     * @param bf 目标字节流
     */
	public static void putToBuffer(Object v, ByteBuffer bf) {
		Class<?> objClass = v.getClass();
		if (objClass.isArray()){
			Object[] arrayValue = (Object[])v;
			for (int i = 0; i < arrayValue.length; i++) {
				putToBuffer(arrayValue[i], bf);
			}
		}
		else if (objClass.equals(String.class)){
			bf.put(((String)v).getBytes());
		}
		else if (objClass.equals(Long.class)){
			bf.putLong(((Long)v));
		}
		else if (objClass.equals(Integer.class)){
			bf.putInt(((Integer)v));
		}
		else if (objClass.equals(Short.class)){
			bf.putShort(((Short)v));
		}
		else if (objClass.equals(Byte.class)){
			bf.put(((Byte)v));
		}
		else if (MsgStruct.class.isAssignableFrom(objClass)){
			((MsgStruct)v).writeToBuffer(bf);
		}
	}
	
	public static boolean isEmpty(String s){
		return s == null || "".equals(s.trim());
	}
}
